package com.sanctuary.models;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.solr.client.solrj.beans.Field;

import com.sanctuary.annotations.SolrArray;
import com.sanctuary.annotations.SolrField;
import com.sanctuary.interfaces.Model;

public class Term implements Model {
	
	@SolrField("id")
	int id;
	
	/**
	 * This is a stupid hack in that the solrj client can't seem to 
	 * deal with anything that isn't a string. So, this field is
	 * set as a string
	 */
	@Field("id")
	String tid;
	
	@Field
	@SolrField("term")
	String term;
	
	@Field
	@SolrField("weight")
	double weight;
	
	private Date dateAdded;
	private Date dateLastModified;
	
	@SolrField("cats")
	@SolrArray
	List<TermCategory> categories;
	
	/**
	 * This is just for solr, because its indexing engine doesn't like
	 * nested beans (TermCategory) so the indexer will create a String
	 * array with the category names.
	 */
	@Field("cat")
	Object[] cats;
	
	public Term() {
		categories = new ArrayList<TermCategory>();
	}
	
	public String getTid() {
		return tid;
	}

	public void setTid(String tid) {
		this.tid = tid;
		// also, because solrj doesn't like ints, lets set our int ourselves!
		this.setId(Integer.parseInt(tid));
	}

	public void addCategory(String cat) {
		TermCategory tc = new TermCategory();
		tc.setName(cat);
		categories.add(tc);
	}
	
	public Object[] getCats() {
		return cats;
	}

	public void setCats(Object[] cats) {
		this.cats = cats;
		for (Object c : cats) {
			this.addCategory(c.toString());
		}
	}

	public List<TermCategory> getCategories() {
		return categories;
	}

	public void setCategories(List<TermCategory> categories) {
		this.categories = categories;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTerm() {
		return term;
	}

	public void setTerm(String term) {
		this.term = term;
	}

	public double getWeight() {
		return weight;
	}

	public void setWeight(double weight) {
		this.weight = weight;
	}

	public Date getDateAdded() {
		return dateAdded;
	}

	public void setDateAdded(Date dateAdded) {
		this.dateAdded = dateAdded;
	}

	public Date getDateLastModified() {
		return dateLastModified;
	}

	public void setDateLastModified(Date dateLastModified) {
		this.dateLastModified = dateLastModified;
	}

	public String getAliasName() {
		// TODO Auto-generated method stub
		return "term";
	}

}
